**Setting up country-year party id
gen rparty_id_g=pf_party_id
**Addressing issue where the ruling party is the winner of previous election
replace rparty_id_g=rparty_id_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_id_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_id_g=rparty_id_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_id_g[_n-1]~=rparty_id_g





**Setting up country-year party vote share
gen rparty_vote_g=v2pavote
**Addressing issue where the ruling party is the winner of previous election
replace rparty_vote_g=rparty_vote_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_vote_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_vote_g=rparty_vote_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_vote_g[_n-1]~=rparty_vote_g



**Setting up country-year party seat share
gen rparty_seat_g=v2paseatshare
**Addressing issue where the ruling party is the winner of previous election
replace rparty_seat_g=rparty_seat_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_seat_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_seat_g=rparty_seat_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_seat_g[_n-1]~=rparty_seat_g


***Total strength
gen share_g=rparty_seat_g
replace share_g=rparty_vote_g if share==. 




**Setting up country-year local org strength
gen rparty_lorg_g=v2paactcom
**Addressing issue where the ruling party is the winner of previous election
replace rparty_lorg_g=rparty_lorg_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_lorg_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_lorg_g=rparty_lorg_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_lorg_g[_n-1]~=rparty_lorg_g



**Setting up country-year personalization strength
gen rparty_pers_g=v2paind
**Addressing issue where the ruling party is the winner of previous election
replace rparty_pers_g=rparty_pers_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_pers_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_pers_g=rparty_pers_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_pers_g[_n-1]~=rparty_pers_g



**Setting up country-year cohesion
gen rparty_cohes_g=v2padisa
**Addressing issue where the ruling party is the winner of previous election
replace rparty_cohes_g=rparty_cohes_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_cohes_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_cohes_g=rparty_cohes_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_cohes_g[_n-1]~=rparty_cohes_g


**Setting up country-year cohesion ordinal
gen rparty_cohes_g_ord=v2padisa_ord
**Addressing issue where the ruling party is the winner of previous election
replace rparty_cohes_g_ord=rparty_cohes_g_ord[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_cohes_g_ord, replace
sort cowcode year





***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_cohes_g_ord=rparty_cohes_g_ord[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_cohes_g_ord[_n-1]~=rparty_cohes_g_ord


gen defections_g=1 if rparty_cohes_g_ord==0 | rparty_cohes_g_ord==1
replace defections_g=0 if rparty_cohes_g_ord>1 & rparty_cohes_g_ord<5

 


**Setting up country-year clientelism
gen rparty_client_g=v2paclient
**Addressing issue where the ruling party is the winner of previous election
replace rparty_client_g=rparty_client_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_client_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_client_g=rparty_client_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_client_g[_n-1]~=rparty_client_g







**Setting up country-year local offices
gen rparty_off_g=v2palocoff
**Addressing issue where the ruling party is the winner of previous election
replace rparty_off_g=rparty_off_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_off_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_off_g=rparty_off_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_off_g[_n-1]~=rparty_off_g




**Setting up country-year social org. ties
gen rparty_soc_g=v2pasoctie
**Addressing issue where the ruling party is the winner of previous election
replace rparty_soc_g=rparty_soc_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_soc_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_soc_g=rparty_soc_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_soc_g[_n-1]~=rparty_soc_g




**Setting up country-year candidate nominations 
gen rparty_nom_g=v2panom
**Addressing issue where the ruling party is the winner of previous election
replace rparty_nom_g=rparty_nom_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_nom_g, replace
sort cowcode year





***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_nom_g=rparty_nom_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_nom_g[_n-1]~=rparty_nom_g


**Binary 
gen leader_nominates_g=1 if rparty_nom_g==0
replace leader_nominates_g=0 if rparty_nom_g~=0 & rparty_nom_g~=.



**Setting up country-year right/left
gen rparty_rl_g=v2pariglef
**Addressing issue where the ruling party is the winner of previous election
replace rparty_rl_g=rparty_rl_g[_n-1] if g_auth==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_rl_g, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_rl_g=rparty_rl_g[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_rl_g[_n-1]~=rparty_rl_g



**Other
replace g_auth=0 if year==2008 & cowcode==771


gen marx_g=marx
replace marx_g=0 if marx_g==. & g_auth==1 
replace marx_g=1 if cowcode==110 & year==1992
replace marx_g=1 if cowcode==101 & year>2000 & year<2005
replace marx_g=1 if cowcode==290 & year==1989
replace marx_g=1 if cowcode==310 & year==1990
replace marx_g=1 if cowcode==339 & year==1992
replace marx_g=1 if cowcode==355 & year==1990
replace marx_g=1 if cowcode==360 & year==1990
replace marx_g=1 if cowcode==365 & year>1971 & year<1992
replace marx_g=1 if cowcode==404 & year>1993 & year<1998
replace marx_g=1 if cowcode==439 & year>1971 & year<2016
replace marx_g=1 if cowcode==541 & year>1993 & year<2004
replace marx_g=1 if cowcode==712 & year>1989 & year<1992
replace marx_g=1 if cowcode==402 & year==1991
replace marx_g=1 if cowcode==433 & year==2000
replace marx_g=1 if cowcode==438 & year==1984
replace marx_g=1 if cowcode==520 & year==1991
replace marx_g=1 if cowcode==700 & year==1992
replace marx_g=1 if cowcode==775 & year==1988


**Reverse
gen rparty_nom_rev_g=-1*rparty_nom_g



***Social Org Index
gen org_index_g=rparty_lorg_g+rparty_soc_g+rparty_off_g

**Elite indiex
gen elite_index_g=rparty_nom_rev_g+rparty_pers_g



